﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using SHKF.B2C.BaseHosted;
using SHKF.B2C.BLL.Center;
using SHKF.B2C.Hosted;
using SHKF.B2C.Model.Dto;
using SHKF.B2C.Model.Dto.Center;
using SHKF.B2C.Model.Entity.Center;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks; 

namespace SHKF.B2C.Mannage.Service.Controllers
{
    [Route("[controller]")]
    [ApiController]
    public class ShopController : BaseController
    {
        private readonly ShopBLL shopBLL;
        private readonly ShopMapBLL shopMapBLL; 
        public ShopController(ShopBLL shopBLL, 
            ShopMapBLL shopMapBLL, 
           ILogger<ShopController> logger) : base(logger)
        {
            this.shopBLL = shopBLL;
            this.shopMapBLL = shopMapBLL; 
        }

        [HttpGet("Get")]
        public ShopDto Get(long SN)
        {
            ShopDto dto = new ShopDto();
            try
            {
                dto.Data = this.shopBLL.Get(SN);
                if( dto.Data != null)
                { 
                    dto.Maps = this.shopMapBLL.Query(dto.Data.ShopSN, "");
                }
                if (dto.Maps == null)
                {
                    dto.Maps = new List<ShopMapEntity>();
                }
            }
            catch (Exception ex)
            {
                dto = new ShopDto();
                dto.SetException(ex);
                base.WriteException("Get", ex);
            }

            return dto;
        }

        [HttpGet("map/list")]
        public ShopMapsDto MapList(long ShopSN)
        {
            ShopMapsDto dto = new ShopMapsDto();
            try
            {
                dto.data = this.shopMapBLL.Query(ShopSN,""); 
                if (dto.data != null)
                {
                    //加载美团配送的数据
                    //ShopMtpsEntity shop = shopMtpsBLL.Get(ShopSN.ToString());
                    //if (shop != null)
                    //{
                    //    dto.Mtps = shop;
                    //}
                }
            }
            catch (Exception ex)
            {
                dto = new ShopMapsDto();
                dto.SetException(ex); 
                base.WriteException("CList", ex);
            } 
            return dto;
        }

        [HttpPost("map/save")]
        public DataDto MapSave(ShopMapEntity shopMapEntity)
        {
            //门店映射
            DataDto dto = new DataDto();
            try
            {
               int iresult = this.shopMapBLL.Save(shopMapEntity); 
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("CList", ex);
            }
            return dto;
        }

        [HttpPost("map/save/batch")]
        public DataDto MapSaveBatch(List<ShopMapEntity> shopMapList)
        {
            //门店映射
            DataDto dto = new DataDto();
            try
            {
                if (shopMapList != null)
                {
                    foreach (ShopMapEntity shopMapEntity in shopMapList)
                    {
                        int iresult = this.shopMapBLL.Save(shopMapEntity);
                    }
                }
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("MapSaveBatch", ex);
            }
            return dto; 
        }

        [HttpGet("map/delete")]
        public DataDto MapDelete(long SN)
        {
            DataDto dto = new DataDto();
            try
            {
               this.shopMapBLL.Delete(SN);
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("CList", ex);
            }
            return dto;
        }

        [HttpGet("list")]
        public ShopListDto List(int CompanySN, string ShopName)
        {
            ShopListDto dto = new ShopListDto(); 
            try
            {  
                dto.data = this.shopBLL.Query(CompanySN, ShopName);
            }
            catch (Exception ex)
            {
                dto.error = new Error() { code = -1, msg = ex.Message };
                base.WriteException("Query", ex);
            }

            return dto;
        }

        [HttpPost("save")]
        public ShopDto Save(ShopEntity shopEntity)
        {
            ShopDto dto = new ShopDto();
            try
            {
                dto.Data = this.shopBLL.Save(shopEntity);
            }
            catch (Exception ex)
            {
                dto = new ShopDto();
                dto.SetException(ex);
                base.WriteException("CList", ex);
            }
            return dto;
        }

        [HttpGet("Delete")]
        public DataDto Delete(long SN)
        {
            DataDto dto = new DataDto();
            try
            {
                ShopEntity shop = this.shopBLL.Get(SN);
                if (shop != null)
                {
                    this.shopBLL.Delete(SN);
                    this.shopMapBLL.DeleteShop(shop.ShopSN); 
                } 
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("CList", ex);
            }
            return dto;
        }

        [HttpGet("yzy.kdt.id")]
        public DataDto GetYzyKdtId(long CompanySN)
        {
            DataDto dto = new DataDto();
            try
            {
               long id = this.shopMapBLL.GetKdtId(CompanySN); 
                dto.data = id.ToString();
            }
            catch (Exception ex)
            {
                dto = new DataDto();
                dto.SetException(ex);
                base.WriteException("CList", ex);
            }
            return dto;
        }

    }
}
